/**
 * Copyright (c) 2018 qar All rights reserved.
 *
 * https://www.qar.io
 *
 * 版权所有，侵权必究！
 */

package com.example.controller;

import com.example.dto.SysDictDataDTO;
import com.example.model.api.Result;
import com.example.model.page.PageData;
import com.example.service.SysDictDataService;
import com.example.validator.AssertUtils;
import com.example.validator.ValidatorUtils;
import com.example.validator.group.DefaultGroup;
import com.example.validator.group.UpdateGroup;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 字典数据
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("dict/data")
@Tag(name = "字典数据")
public class SysDictDataController {
    @Autowired
    private SysDictDataService sysDictDataService;

    @RequestMapping(value = "page", method = {RequestMethod.POST, RequestMethod.GET})
    @Operation(summary = "分页")
    public Result<PageData<SysDictDataDTO>> page(@RequestParam Map<String, Object> params) {
        PageData<SysDictDataDTO> page = sysDictDataService.page(params);
        return new Result<PageData<SysDictDataDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    public Result<SysDictDataDTO> get(@PathVariable("id") Long id) {
        SysDictDataDTO data = sysDictDataService.get(id);
        return new Result<SysDictDataDTO>().ok(data);
    }

    @PostMapping
    @Operation(summary = "保存")
    public Result save(@RequestBody SysDictDataDTO dto) {
        ValidatorUtils.validateEntity(dto, DefaultGroup.class);
        sysDictDataService.save(dto);
        return Result.ok();
    }

    @PutMapping
    @Operation(summary = "修改")
    public Result update(@RequestBody SysDictDataDTO dto) {
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);
        sysDictDataService.update(dto);
        return Result.ok();
    }

    @DeleteMapping
    @Operation(summary = "删除")
    public Result delete(@RequestBody Long[] ids) {
        AssertUtils.isArrayEmpty(ids, "数组参数不能为空");
        sysDictDataService.delete(ids);
        return Result.ok();
    }
}